<template>
    <div>
        <div>
            <el-form ref="searchForm" :inline="true" :model="condition.search" label-suffix="" label-width="100px"
                     label-position="left" size="small">
                <el-row>
                    <el-col :span=6>
                        <el-form-item label="唯一ID" prop="id">
                            <el-input v-model="condition.search.id" placeholder="唯一ID"
                                      oninput="value=value.replace(/[^\d]/g,'')"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="应用名称" prop="name">
                            <el-input v-model="condition.search.name" placeholder="应用名称"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="Token" prop="token">
                            <el-input v-model="condition.search.token" placeholder="Token"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="SUITE_ID" prop="suiteId">
                            <el-input v-model="condition.search.suiteId" placeholder="SUITE_ID"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span=6>
                        <el-form-item label="类型" prop="type">
                            <el-select v-model="condition.search.type" placeholder="类型">
                                <el-option label="代开发" value="DAI"></el-option>
                                <el-option label="第三方" value="THIRD"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="所属环境" prop="env">
                            <el-select v-model="condition.search.env" placeholder="环境">
                                <el-option label="生产" value="PRO"></el-option>
                                <el-option label="测试" value="TEST"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col>
                        <el-form-item>
                            <el-button type="primary" @click="getData">查询</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="showAddDialog">新增</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="reset">重置</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="exportExcel">导出</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <el-table
            id="mainTable"
            size="small"
            :data="tableData"
            header-cell-class-name="tableHeaderStyle"
            :row-class-name="tableRowClassName">
            <el-table-column
                prop="id"
                width="40px"
                label="ID">
            </el-table-column>
            <el-table-column
                prop="name"
                label="应用名称">
            </el-table-column>
            <el-table-column
                prop="token"
                label="Token">
            </el-table-column>
            <el-table-column
                prop="encodingAesKey"
                label="AES_KEY">
                <template v-slot="scope">
                    <table-popover :text=scope.row.encodingAesKey></table-popover>
                </template>
            </el-table-column>
            <el-table-column
                prop="type"
                :formatter="formatType"
                width="80px"
                label="类型">
            </el-table-column>
            <el-table-column
                prop="env"
                :formatter="formatEnv"
                label="所属环境">
            </el-table-column>
            <el-table-column
                prop="suiteId"
                label="SUITE_ID">
            </el-table-column>
            <el-table-column
                prop="suiteSecret"
                width="130px"
                label="SUITE_SECRET">
                <template v-slot="scope">
                    <table-popover :text=scope.row.suiteSecret></table-popover>
                </template>
            </el-table-column>
            <el-table-column
                prop="suiteTicket"
                label="Ticket">
                <template v-slot="scope">
                    <table-popover :text=scope.row.suiteTicket></table-popover>
                </template>
            </el-table-column>
            <el-table-column
                prop="accessToken"
                label="AT">
                <template v-slot="scope">
                    <table-popover :text=scope.row.accessToken></table-popover>
                </template>
            </el-table-column>
            <el-table-column
                prop="accessTokenExpiryTime"
                label="AT过期时间">
            </el-table-column>
            <el-table-column
                prop="authQrcode"
                label="授权二维码">
                <template v-slot="scope">
                    <el-image style="width: 60px; height: 60px"
                              v-if="scope.row.authQrcode"
                              :src="scope.row.authQrcode"
                              :preview-src-list="qrcodeUrls">
                        fit="contain">
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                fixed="right"
                width="60">
                <template v-slot="scope">
                    <div>
                        <!--                        <el-button @click="showUpdateDialog(scope.row)" type="text" size="small">编辑</el-button>-->
                        <!--                        <el-button @click="onDelete(scope.row)" type="text" size="small">删除</el-button>-->
                        <el-button @click="onRefresh(scope.row)" type="text" size="small">刷新</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="condition.page"
            :page-sizes="[10, 20, 30, 40, 50, 100]"
            :page-size.sync="condition.rows"
            layout="sizes, prev, pager, next"
            :total.sync="condition.total">
        </el-pagination>
        <el-dialog :title=addOrUpdateDialogTitle width="30%" :visible.sync="addOrUpdateDialogVisible">
            <el-form :model="addOrUpdateForm" label-width="120px">
                <el-form-item label="应用名称">
                    <el-input v-model="addOrUpdateForm.name" placeholder="应用名称"></el-input>
                </el-form-item>
                <el-form-item label="Token">
                    <el-input v-model="addOrUpdateForm.token" placeholder="Token"></el-input>
                </el-form-item>
                <el-form-item label="AES_KEY">
                    <el-input v-model="addOrUpdateForm.encodingAesKey" placeholder="AES_KEY"></el-input>
                </el-form-item>
                <el-form-item label="类型">
                    <el-input v-model="addOrUpdateForm.type" placeholder="类型"></el-input>
                </el-form-item>
                <el-form-item label="所属环境">
                    <el-input v-model="addOrUpdateForm.env" placeholder="所属环境"></el-input>
                </el-form-item>
                <el-form-item label="SUITE_ID">
                    <el-input v-model="addOrUpdateForm.suiteId" placeholder="SUITE_ID"></el-input>
                </el-form-item>
                <el-form-item label="SUITE_SECRET">
                    <el-input v-model="addOrUpdateForm.suiteSecret" placeholder="SUITE_SECRET"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addOrUpdateDialogVisible = false">取 消</el-button>
                <el-button @click="addOrUpdateConfirm" type="primary">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    addWorkAppConfig,
    deleteWorkAppConfig,
    getWorkAppConfigByPage,
    refreshWorkAppConfig,
    updateWorkAppConfig
} from '@/api/api'
import TablePopover from "@/component/single/TablePopover";

export default {
    name: "WorkAppConfig",
    components: {TablePopover},
    data() {
        return {
            // 分页搜索条件
            condition: {
                page: 1,
                rows: 10,
                total: 0,
                search: {}
            },
            // 表格数据
            tableData: [],
            // 新增或者编辑对话框显示
            addOrUpdateDialogVisible: false,
            // 标题
            addOrUpdateDialogTitle: '新增',
            // 新增或者编辑对话框表单
            addOrUpdateForm: {},
            // 选中行
            selectedRow: {},

            // 授权二维码url数组
            qrcodeUrls: [],
        }
    },
    methods: {
        // 这里可以单独设置每一行的样式
        tableRowClassName({row, rowIndex}) {
            return '';
        },
        handleSizeChange(val) {
            this.getData();
        },
        handleCurrentChange(val) {
            this.getData();
        },
        // 格式布尔值
        formatBoolean: function (row, column, cellValue) {
            if (cellValue) {
                return "是";
            } else {
                return "否";
            }
        },
        formatType: function (row, column, cellValue) {
            if (cellValue == 'DAI') {
                return "代开发";
            } else if (cellValue == 'THIRD') {
                return "第三方";
            } else {
                return "未知";
            }
        },
        formatEnv: function (row, column, cellValue) {
            if (cellValue == 'PRO') {
                return "生产";
            } else if (cellValue == 'TEST') {
                return "测试";
            } else {
                return "未知";
            }
        },
        showAddDialog: function () {
            this.addOrUpdateDialogTitle = '新增';
            this.addOrUpdateDialogVisible = true;
            this.addOrUpdateForm = {};
        },
        showUpdateDialog: function (row) {
            this.addOrUpdateDialogTitle = '编辑';
            this.addOrUpdateDialogVisible = true;
            this.selectedRow = row;
            // 深拷贝
            this.addOrUpdateForm = Object.assign({}, row);
        },
        reset: function () {
            this.$refs['searchForm'].resetFields();
        },
        exportExcel: function () {
            this.GLOBAL.exportExcel('#mainTable', '数据.xlsx');
        },
        add: function () {
            addWorkAppConfig(this.addOrUpdateForm).then(data => {
                this.addOrUpdateDialogVisible = false;
                this.getData();
            });
        },
        update: function () {
            updateWorkAppConfig(this.addOrUpdateForm).then(data => {
                this.addOrUpdateDialogVisible = false;
                this.getData();
            });
        },
        onRefresh: function (row) {
            this.$confirm('请确认是否刷新, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                refreshWorkAppConfig({id: row.id}).then(data => {
                    this.$message({
                        type: 'success',
                        message: '刷新成功!'
                    });
                    this.getData();
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        onDelete: function (row) {
            this.$confirm('请确认是否删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteWorkAppConfig({id: row.id}).then(data => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getData();
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        addOrUpdateConfirm: function () {
            if (this.addOrUpdateDialogTitle == '新增') {
                this.add();
            } else if (this.addOrUpdateDialogTitle == '编辑') {
                this.update();
            }
        },
        // 获取表格数据
        getData: function () {
            getWorkAppConfigByPage(this.condition).then(data => {
                this.tableData = data.body.rows;
                this.condition.total = data.body.total;
                this.tableData.forEach((item, index, arr) => {
                    if (item.authQrcode) {
                        this.qrcodeUrls.push(item.authQrcode);
                    }
                })
            });
        },
    },
    created() {
        this.getData();
    },
}
</script>

<style scoped>
.el-input {
    width: 150px !important;
}

.el-select {
    width: 150px !important;
}
</style>
